# -*- coding: utf-8 -*-

# DayRaport implementation generated from reading ui file 'raportDay.ui'
#
# Created: Thu May 31 20:48:40 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.model.Category import Category
from pl.edu.budgetanalizer.GUI.raportsManagment import CategoryWidget
import matplotlib.pyplot as plt
import numpy
import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
    

class Ui_DayRaport(QtGui.QWidget):
    '''
    zawiera slownik {'kategoria': podsuma}
    '''
    listOfProducers = {}  
    visible = False;
    x_tup = ()
    y_tup = ()
    
    def getAllCategories(self):
        '''
        pobiera wszystkie kategorie i ich podsumy, wsadza do slownika
        '''
        self.listOfProducers.clear()
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("select * from Categories")

        x_categorie = []
        y_ammount = []  
        
        rows = cur.fetchall()
        for row in rows:
            category = Category(row.name)
            x_categorie.append(category.name)
            start = str(self.startdatafield.date())
            start = start[start.find("(")+1:start.find(")")]
            start = start.replace(", ", "-")
            stop = str(self.enddatafield.date())
            stop = stop[stop.find("(")+1:stop.find(")")]
            stop = stop.replace(", ", "-")
            cur.execute("select dbo.getAmountForCategoryForData(?, ? ,?)",category.name, start , stop )
            row = cur.fetchone()
            amount = row[0]
            if amount:
                self.listOfProducers[category] = amount
            else:
                self.listOfProducers[category] = .0
            if(amount != None):
                y_ammount.append(amount)
            else:
                y_ammount.append(0)
                
        self.x_tup = tuple(x_categorie)
        self.y_tup = tuple(y_ammount) 

            
           
        conn.close()
    
    def addCategory(self,category):
        
        categoryButton = QtGui.QPushButton(self.horizontalLayoutWidget_2)
        categoryButton.setMinimumSize(QtCore.QSize(0, 30))
        categoryButton.setObjectName(_fromUtf8("categoryButton"))
        categoryButton.connect(categoryButton, QtCore.SIGNAL("clicked()"),self.openCategoryWidget)
        self.categoryVerticalLayout.addWidget(categoryButton)
            
        priceHorizontalLayout = QtGui.QHBoxLayout()
        priceHorizontalLayout.setSizeConstraint(QtGui.QLayout.SetMaximumSize)
        priceHorizontalLayout.setContentsMargins(-1, -1, 10, -1)
        priceHorizontalLayout.setObjectName(_fromUtf8("priceHorizontalLayout"))
            
        priceLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        priceLabel.setMaximumSize(QtCore.QSize(16777215, 20))
        priceLabel.setObjectName(_fromUtf8("priceLabel"))
        priceHorizontalLayout.addWidget(priceLabel)
        measureLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        measureLabel.setMaximumSize(QtCore.QSize(16777215, 20))
        measureLabel.setObjectName(_fromUtf8("measureLabel"))
        priceHorizontalLayout.addWidget(measureLabel)
        self.realtVerticalLayout.addLayout(priceHorizontalLayout)
            
        categoryButton.setText(category.name)
           
        priceLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">" +QtCore.QString().setNum(self.listOfProducers[category])+ "</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
            
        measureLabel.setText(QtGui.QApplication.translate("DayRaport", "zl", None, QtGui.QApplication.UnicodeUTF8))
            
        
    def openCategoryWidget(self):
        self.category = Category(str(self.sender().text()))
        self.categoryWidget = CategoryWidget.Ui_MainWindow()
        self.categoryWidget.setupUi(self.categoryWidget,self.category,self.databaseDir)
        self.categoryWidget.setGeometry(QtCore.QRect(0, 0, 400, 120))
        self.categoryWidget.setAutoFillBackground(True)
        self.categoryWidget.setGeometry(QtCore.QRect(360,180,420,400))
        self.categoryWidget.show()
        
        
    def genRaport(self):
        self.getAllCategories()
        for category in self.listOfProducers:
            self.addCategory(category)
        self.drawGraph()
        self.listOfProducers.clear()  
        self.x_tup = ()
        self.y_tup = ()
    
            
    def drawGraph(self):
        ind = numpy.arange(len(self.x_tup))
        width = 0.35
        plt.bar(ind, self.y_tup, width, color='r', bottom = 0 , yerr=None)
        plt.ylabel('Podsuma')
        plt.xlabel('Kategoria')
        plt.xticks(ind+width/2., self.x_tup )
        plt.yticks(numpy.arange(0,max(self.y_tup),10))
        plt.show()

        
    def setupUi(self, DayRaport, date,databaseDir):
        '''
        wygenerowanie przez gui
        '''
       
       
        self.databaseDir = databaseDir
        DayRaport.resize(431, 491)
        self.horizontalLayoutWidget = QtGui.QWidget(DayRaport)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 401, 41))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        
        #data
        self.startdatafield = QtGui.QDateEdit(DayRaport)
        self.startdatafield.setGeometry(QtCore.QRect(0, 50, 200, 20))
        self.startdatafield.setMinimumDate(QtCore.QDate.currentDate().addDays(-1000))
        self.startdatafield.setMaximumDate(QtCore.QDate.currentDate().addDays(1000))
        self.startdatafield.show()
       
        self.enddatafield = QtGui.QDateEdit(DayRaport)
        self.enddatafield.setGeometry(QtCore.QRect(201, 50, 200, 20))
        self.enddatafield.setMinimumDate(QtCore.QDate.currentDate().addDays(-1000))
        self.enddatafield.setMaximumDate(QtCore.QDate.currentDate().addDays(1000))
        self.enddatafield.show()
        
        self.genButton = QtGui.QPushButton(DayRaport)
        self.genButton.setMinimumSize(QtCore.QSize(0, 30))
        self.genButton.setGeometry(401, 50, 40, 20)
        self.genButton.setObjectName(_fromUtf8("genButton"))
        self.genButton.connect(self.genButton, QtCore.SIGNAL("clicked()"),self.genRaport)

        
        
        #raport label
        self.raportHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.raportHorizontalLayout.setMargin(0)
        self.raportHorizontalLayout.setObjectName(_fromUtf8("raportHorizontalLayout"))
        self.raportLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.raportLabel.setObjectName(_fromUtf8("raportLabel"))
        self.raportHorizontalLayout.addWidget(self.raportLabel)
        self.dataLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.dataLabel.setObjectName(_fromUtf8("dataLabel"))
        self.raportHorizontalLayout.addWidget(self.dataLabel)
        
#        #wykres
#        self.chartWidget = QtGui.QWidget(DayRaport)
#        self.chartWidget.setGeometry(QtCore.QRect(20, 290, 381, 171))
#        self.chartWidget.setObjectName(_fromUtf8("chartWidget"))

        #polaczenie
        self.horizontalLayoutWidget_2 = QtGui.QWidget(DayRaport)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(19, 99, 381, 182))
        self.horizontalLayoutWidget_2.setObjectName(_fromUtf8("horizontalLayoutWidget_2"))
        self.togetherHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.togetherHorizontalLayout.setMargin(0)
        self.togetherHorizontalLayout.setObjectName(_fromUtf8("togetherHorizontalLayout"))

        
        #kategorie
        self.categoryVerticalLayout = QtGui.QVBoxLayout()
        self.categoryVerticalLayout.setObjectName(_fromUtf8("categoryVerticalLayout"))
        self.noneLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.noneLabel.setMaximumSize(QtCore.QSize(0, 30))
        self.noneLabel.setObjectName(_fromUtf8("noneLabel"))
        self.categoryVerticalLayout.addWidget(self.noneLabel)
        
        self.realtVerticalLayout = QtGui.QVBoxLayout()
        self.realtVerticalLayout.setObjectName(_fromUtf8("realtVerticalLayout"))
        self.amountLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.amountLabel.setMaximumSize(QtCore.QSize(16777215, 30))
        self.amountLabel.setObjectName(_fromUtf8("amountLabel"))
        self.realtVerticalLayout.addWidget(self.amountLabel)

                


        self.togetherHorizontalLayout.addLayout(self.categoryVerticalLayout)
        self.togetherHorizontalLayout.addLayout(self.realtVerticalLayout)

        self.retranslateUi(DayRaport)
        QtCore.QMetaObject.connectSlotsByName(DayRaport)

    def retranslateUi(self, DayRaport):
        DayRaport.setWindowTitle(QtGui.QApplication.translate("DayRaport", "DayRaport", None, QtGui.QApplication.UnicodeUTF8))
        self.raportLabel.setText(QtGui.QApplication.translate("DayRaport", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:18pt;\">Raport:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.amountLabel.setText(QtGui.QApplication.translate("DayRaport", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt;\">Podsuma</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
        self.noneLabel.setText(QtGui.QApplication.translate("DayRaport", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:11pt;\">   </span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
    